//
//  Node.swift
//  数据结构与算法
//
//  Created by Rowling on 2024/2/22.
//

import Foundation
class Node<T> {
    init(element: T, left: Node? = nil, right: Node? = nil, parent: Node? = nil) {
        self.element = element
        self.left = left
        self.right = right
        self.parent = parent
    }
    var element : T
    var left : Node?
    var right : Node?
    var parent : Node?

    var isLeaf : Bool {
        left == nil && right == nil
    }
    
    var subNodeCount : Int {
        var d = 0
        if left != nil {
            d += 1
        }
        if right != nil {
            d += 1
        }
        return d
    }
    
    var isLeft : Bool {
        parent?.left === self
    }
    
    var isRight : Bool {
        parent?.right === self
    }
    
    func sibling() -> Node? {
        if isLeft {
            return parent!.right
        } else if isRight {
            return parent!.left
        }
        return nil
    }
    
    
    deinit {
        print("节点\(element)销毁")
    }
}
